import {
	network
} from "./config.js"
import vue from 'vue'
const baseurl = network + '/api';



const request = (obj, type, api) => {
	const token = uni.getStorageSync('token');
	if (token) {
		if (!obj.header) {
			obj.header = {}
		}
		obj.header.token = token;
	}
	return new Promise(function(reslove, reject) {
		uni.request({
			url: baseurl + obj.url,
			data: obj.data,
			method: type,
			header: obj.header ? obj.header : {
				"Content-Type": "application/json"
			},

			success(res) {
				if (res.data.code === 1 || res.data.code === 10001) {
					reslove(res.data);
				} else if (res.data.code == -1) {
					// 失效立即更新token 并再次请求
					uni.removeStorageSync("token")
					// uni.removeStorageSync("userInfo")
					var userInfo = uni.getStorageSync('userInfo')
					if (!!userInfo) {
						uni.removeStorageSync("userInfo")
					}
					vue.prototype.loginCom.open()
				} else {
					if (obj.divMsg) {
						if (obj.title) {
							uni.showToast({
								icon: 'none',
								title: obj.title
							});
						}
						return
					}
					uni.showToast({
						icon: 'none',
						title: res.data.msg
					});
				}

				// uni.navigateTo({
				// 	url: "/pages/login/login",
				// })

			},

			fail(err) {
				console.log(err)
				uni.showToast({
					icon: 'none',
					title: '网络错误，请稍后再试'
				});
				reject(err);
			}
		});
	});
};

const get = obj => {
	return request(obj, 'GET');
};


const post = obj => {
	return request(obj, 'POST');
};

const uploadImage = (obj) => {
	const token = uni.getStorageSync('token');
	return new Promise((reslove, reject) => {
		uni.uploadFile({
			...obj.data,
			url: baseurl + obj.url,
			header: {
				token,
			},
			success: (uploadFileRes) => {
				console.log(uploadFileRes)
				if (JSON.parse(uploadFileRes.data).code == -1) {
					vue.prototype.loginCom.open()
					reject(uploadFileRes)
					return
				}
				reslove(uploadFileRes.data)
			},
			fail: (e) => {

				reject(e)
			}
		});
	});
}

const upload = (url, name, tempFilePath) => {
	const token = uni.getStorageSync('token');
	return new Promise((reslove, reject) => {
		uni.uploadFile({
			url: baseurl + url,
			filePath: tempFilePath,
			name: 'file',
			header: {
				'token': token
			},
			success: (uploadFileRes) => {
				if (JSON.parse(uploadFileRes.data).code == -1) {
					vue.prototype.loginCom.open()
					reject(uploadFileRes)
					return
				}
				reslove(JSON.parse(uploadFileRes.data).data);
			},
			fail: (e) => {
				reject(e)
			}
		});
	});
}

const uploadFile = (Filetype = "image", url) => {

	return new Promise((reslove, reject) => {
		switch (Filetype) {
			case "image":
				uni.chooseImage({
					sourceType: ['camera', 'album'],
					count: 1,
					success: async (chooseImageRes) => {
						console.log(chooseImageRes)
						const tempFilePaths = chooseImageRes.tempFilePaths;
						const res = await upload(url, name, tempFilePaths[0])
						reslove(res)
					}
				});
				break;
			case "video":
				uni.chooseVideo({
					sourceType: ['camera', 'album'],
					success: async (videorRes) => {
						console.log(videorRes)
						const res = await upload(url, name, videorRes.tempFilePath);
						reslove(res)
					}
				});
				break;
			default:
				uni.chooseFile({
					count: 1,
					 extension:['.'+Filetype],
					success: async (FileRes) => {
						console.log( FileRes,"FileRes.tempFilePaths[0]")
						const tempFile = FileRes.tempFilePaths[0]
						const res = await upload(url, FileRes.tempFiles[0].name, tempFile);
						reslove(res)
					}
				})
				break;
		}


	});
}
module.exports = {
	post,
	get,
	uploadImage,
	uploadFile,

};